//依赖
const gulp = require("gulp");
const connect = require("gulp-connect");
var sass = require('gulp-sass');
sass.compiler = require('node-sass');
var proxy = require('http-proxy-middleware');

const scssJSON  = require("./conf/index").scss;
const scripts  = require("./conf/index").scripts;
const proxyList = require("./conf/index").proxyList;

const gpProxy = require("./utils/index").gpProxy;

const {
    watchScripts,
    watchScss,
    mergeMyScripts,
    mergeMyScss
} = require('./utils/index');

let [scriptsTaskArray,scssTaskArray] = [];


//路径配置 获取当前路径下所有文件路径
function init(){
    scriptsTaskArray = mergeMyScripts(scripts,'scripts-')
    scssTaskArray = mergeMyScss(scssJSON,'scss-')
}



//转存html
gulp.task('html',()=>{
    return gulp.src(['./src/*.html'])
    .pipe(gulp.dest('./dev'))
    .pipe(connect.reload());
})

//监听html改变
gulp.task('watch',()=>{
    gulp.watch('./src/*.html',['html']);
    gulp.watch("./src/scss/index/*.scss",["scss"])
    watchScripts(scripts,'scripts-');
    watchScss(scssJSON,'scss-');
})

//建立服务器
gulp.task('connect',()=>{
    connect.server({
        root:'dev',
        livereload:true,
        middleware: function(connect, opt) {
            return gpProxy(proxyList)
        }
    });
})

init()

//总合并js
gulp.task('scripts',scriptsTaskArray)
//总合并scss
gulp.task('scss',scssTaskArray)

//启动服务器
//gulp.task("default",["html",'scripts',"connect","watch"]);

gulp.task("gulp-dev",["html",'scripts','scss',"connect","watch"]);


require('./gulp-build');